<template>
  <div class="outer-container">
    <div class="container">
      <div class="login-container">
        <div class="header">
          <div class="icon-box">
            <div class="pulse"></div>
            <svg
              xmlns="http://www.w3.org/2000/svg"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              class="beat"
            >
              <path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
              <circle cx="8.5" cy="7" r="4"></circle>
              <line x1="20" y1="8" x2="20" y2="14"></line>
              <line x1="23" y1="11" x2="17" y2="11"></line>
            </svg>
          </div>
        </div>
        <h1>账号注册</h1>
        <p class="subtitle">心理健康管理系统平台</p>
        <el-form
          :model="registerForm"
          :rules="rules"
          ref="registerFormRef"
          @submit.prevent="handleRegister"
        >
          <!-- 用户名 -->
          <el-form-item prop="name">
            <el-input
              v-model="registerForm.name"
              placeholder="请输入姓名"
              :prefix-icon="User"
              clearable
              class="rounded-input"
            ></el-input>
          </el-form-item>

          <!-- 账号 -->
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入账号"
              :prefix-icon="User"
              clearable
              class="rounded-input"
            ></el-input>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              placeholder="请输入密码"
              :type="passwordVisible ? 'text' : 'password'"
              :prefix-icon="Lock"
              class="rounded-input"
            >
              <template #suffix>
                <span
                  v-if="registerForm.password"
                  class="custom-password-toggle"
                  @mousedown.prevent="togglePasswordVisibility"
                >
                  <el-icon>
                    <component :is="passwordVisible ? View : Hide" />
                  </el-icon>
                </span>
              </template>
            </el-input>
          </el-form-item>

          <!-- 确认密码 -->
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              placeholder="请确认密码"
              :type="confirmPasswordVisible ? 'text' : 'password'"
              :prefix-icon="Lock"
              class="rounded-input"
            >
              <template #suffix>
                <span
                  v-if="registerForm.confirmPassword"
                  class="custom-password-toggle"
                  @mousedown.prevent="toggleConfirmPasswordVisibility"
                >
                  <el-icon>
                    <component :is="confirmPasswordVisible ? View : Hide" />
                  </el-icon>
                </span>
              </template>
            </el-input>
          </el-form-item>

          <!-- 身份选择 -->
          <el-form-item class="role-selection">
            <div class="role-options-row">
              <div
                class="role-option"
                :class="{
                  selected: registerForm.role === 'USER',
                }"
                @click="selectRole('USER')"
              >
                <div class="role-radio">
                  <div class="radio-outer">
                    <div class="radio-inner" v-if="registerForm.role === 'USER'"></div>
                  </div>
                </div>
                <div class="role-label">普通用户</div>
              </div>
              <div
                class="role-option"
                :class="{
                  selected: registerForm.role === 'PSYCHOLOGIST',
                }"
                @click="selectRole('PSYCHOLOGIST')"
              >
                <div class="role-radio">
                  <div class="radio-outer">
                    <div class="radio-inner" v-if="registerForm.role === 'PSYCHOLOGIST'"></div>
                  </div>
                </div>
                <div class="role-label">心理医生</div>
              </div>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              class="btn-login"
              native-type="submit"
              :loading="loading"
              round
            >
              {{ loading ? '注册中...' : '注册' }}
            </el-button>
          </el-form-item>
        </el-form>
        <div class="bottom-options">
          <div>已有账号？<el-link type="primary" @click="toLogin">返回登录</el-link></div>
        </div>
        <div class="copyright">
          版权所有 © {{ new Date().getFullYear() }} 心理健康管理系统 - 保留所有权利
        </div>
      </div>
      <div class="decoration-container">
        <img
          src=""
          class="illustration"
          alt="Psychology Illustration"
        />
        <div class="motto">
          <h3>加入我们，关爱心灵健康</h3>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/user'
import { ElMessage } from 'element-plus'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const registerFormRef = ref<FormInstance>()
const passwordVisible = ref(false)
const confirmPasswordVisible = ref(false)

interface RegisterForm {
  name: string
  username: string
  password: string
  confirmPassword: string
  role: string
}

const registerForm = reactive<RegisterForm>({
  name: '',
  username: '',
  password: '',
  confirmPassword: '',
  role: 'USER',
})

// 表单验证规则
const validateName = (rule: any, value: string, callback: any) => {
  if (!value) {
    callback(new Error('请输入姓名'))
  } else if (/^\d+$/.test(value)) {
    callback(new Error('姓名不能为纯数字'))
  } else if (value.length > 12) {
    callback(new Error('姓名不能超过12个字符'))
  } else {
    callback()
  }
}

const validateUsername = (rule: any, value: string, callback: any) => {
  if (!value) {
    callback(new Error('请输入账号'))
  } else if (value.length < 5) {
    callback(new Error('账号不能小于5位'))
  } else if (value.length > 12) {
    callback(new Error('账号不能大于12位'))
  } else if (/[\u4e00-\u9fa5]/.test(value)) {
    callback(new Error('账号不能包含汉字'))
  } else {
    callback()
  }
}

const validatePassword = (rule: any, value: string, callback: any) => {
  if (!value) {
    callback(new Error('请输入密码'))
  } else if (value.length < 6) {
    callback(new Error('密码不能小于6位'))
  } else {
    callback()
  }
}

const validateConfirmPassword = (rule: any, value: string, callback: any) => {
  if (!value) {
    callback(new Error('请确认密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const rules = reactive<FormRules>({
  name: [{ validator: validateName, trigger: 'blur' }],
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }],
  confirmPassword: [{ validator: validateConfirmPassword, trigger: 'blur' }],
})

const togglePasswordVisibility = () => {
  passwordVisible.value = !passwordVisible.value
}

const toggleConfirmPasswordVisibility = () => {
  confirmPasswordVisible.value = !confirmPasswordVisible.value
}

const selectRole = (role: string) => {
  registerForm.role = role
}

const handleRegister = async () => {
  if (!registerFormRef.value) return

  try {
    // 表单验证
    await registerFormRef.value.validate()

    loading.value = true
    const response = await register({
      userName: registerForm.name,
      accountId: registerForm.username,
      password: registerForm.password,
      role: registerForm.role,
    })

    if (response.code === 200) {
      ElMessage.success('注册成功，请登录')
      router.push('/login')
    } else {
      ElMessage.error(response.msg || '注册失败，请重试')
    }
  } catch (error: any) {
    console.error('注册失败', error)
    if (error.message) {
      ElMessage.error(error.message)
    } else {
      ElMessage.error('注册失败，请重试')
    }
  } finally {
    loading.value = false
  }
}

const toLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Microsoft YaHei', sans-serif;
}

.outer-container {
  min-height: 100vh;
  background: #4086f4;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.container {
  width: 90%;
  max-width: 1100px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  position: relative;
  margin: 0 auto;
  display: flex;
  flex-direction: row-reverse;
}

.login-container {
  width: 480px;
  padding: 60px;
  position: relative;
  z-index: 1;
  background: #fff;
  display: flex;
  flex-direction: column;
}

.decoration-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(45deg, #ebf3ff 0%, #ffffff 100%);
  position: relative;
  overflow: hidden;
  min-height: 500px;
}

.header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 60px;
}

.icon-box {
  width: 64px;
  height: 64px;
  background-color: #f6f9ff;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.icon-box .beat {
  width: 32px;
  height: 32px;
  color: #4086f4;
}

.icon-box .pulse {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #4086f4;
  opacity: 0.2;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(0.9);
    opacity: 0.2;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.4;
  }
  100% {
    transform: scale(0.9);
    opacity: 0.2;
  }
}

.beat {
  animation: beat 2s infinite;
  transform-origin: center;
}

@keyframes beat {
  0% {
    transform: scale(1);
  }
  15% {
    transform: scale(1.15);
  }
  30% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.15);
  }
  60% {
    transform: scale(1);
  }
  100% {
    transform: scale(1);
  }
}

h1 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 16px;
  color: #333;
  text-align: center;
}

.subtitle {
  font-size: 16px;
  color: #666;
  margin-bottom: 32px;
  text-align: center;
}

/* 圆角输入框样式 */
.rounded-input :deep(.el-input__wrapper) {
  border-radius: 20px;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

.rounded-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #4086f4 inset;
}

.custom-password-toggle {
  cursor: pointer;
  color: #909399;
}

.custom-password-toggle:hover {
  color: #409eff;
}

.btn-login {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  margin-top: 10px;
  border-radius: 20px;
}

.bottom-options {
  display: flex;
  justify-content: center;
  margin-top: 24px;
  font-size: 14px;
  color: #666;
}

.copyright {
  text-align: center;
  margin-top: 32px;
  font-size: 12px;
  color: #999;
}

.illustration {
  width: 80%;
  max-width: 300px;
  margin-bottom: 20px;
}

.motto {
  margin-top: 16px;
}

.motto h3 {
  color: #4086f4;
  font-size: 24px;
  font-weight: 500;
  text-align: center;
  font-style: italic;
}

/* 身份选择样式 */
.role-selection {
  margin-bottom: 24px;
}

.role-options-row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  background-color: #f6f9ff;
  border-radius: 20px;
  padding: 8px;
  width: 100%;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

.role-option {
  cursor: pointer;
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-radius: 6px;
  transition: all 0.3s ease;
  margin: 0 4px;
  flex: 1;
  justify-content: center;
}

.role-option.selected {
  background-color: rgba(64, 134, 244, 0.15);
}

.role-radio {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  transition: all 0.2s ease;
}

.role-option.selected .role-radio {
  border-color: #4086f4;
}

.radio-outer {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radio-inner {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #4086f4;
}

.role-label {
  font-size: 14px;
  color: #333;
  font-weight: 400;
}

.role-option:hover {
  background-color: rgba(64, 134, 244, 0.08);
}

/* Element Plus表单样式调整 */
:deep(.el-form) {
  display: flex;
  flex-direction: column;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item__content) {
  display: block;
}

@media (max-width: 900px) {
  .container {
    flex-direction: column;
  }

  .login-container {
    width: 100%;
    order: 2;
  }

  .decoration-container {
    order: 1;
    min-height: 200px;
  }
}

@media (max-width: 600px) {
  .login-container {
    padding: 30px;
  }

  .icon-box {
    width: 50px;
    height: 50px;
  }

  .icon-box .beat {
    width: 24px;
    height: 24px;
  }

  h1 {
    font-size: 24px;
  }

  .subtitle {
    font-size: 14px;
  }

  .btn-login {
    height: 44px;
    font-size: 14px;
  }
}
</style>
